package com.secsbrain.development.web.controller;

import com.secsbrain.common.ResponseResult;
import com.secsbrain.deploy.client.iface.DeployService;
import com.secsbrain.deploy.client.model.BuildParam;
import com.secsbrain.development.biz.ICdnRecordService;
import com.secsbrain.development.dal.model.CdnRecord;
import com.secsbrain.sso.core.SsoHelper;
import com.secsbrain.sso.model.dto.system.UserDetailDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.deploy.client.model.JobNameBuildParam;
import com.secsbrain.development.web.dto.CdnDTO;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

@RequestMapping("api/ops")
@RestController
@ApiIgnore
public class OpsController {
	
    private static final Logger logger = LoggerFactory.getLogger(OpsController.class);
    
    @Autowired
    private DeployService deployService;
    
    @Autowired
    private ICdnRecordService cdnRecordService;

	@Autowired
	DeployService deployService2;

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public ResponseResult<?> test(){
		BuildParam buildParam=new BuildParam();
		buildParam.setEnvName("dev");
		buildParam.setAppId(1L);
		buildParam.setBranchName("master");
		ResponseVo<Long> responseVo = deployService2.build(buildParam);
		return new ResponseResult<>(responseVo);
	}

    
    @RequestMapping(value = "/refreshCdn", method = RequestMethod.POST)
    public ResponseResult refreshCdn(@RequestBody CdnDTO cdnDTO) {
    	logger.debug("刷CDN开始。。。参数：{}",JSON.toJSONString(cdnDTO));
    	

    	JobNameBuildParam jobNameBuildParam=new JobNameBuildParam();
    	jobNameBuildParam.setJobName("cdn");
    	jobNameBuildParam.setJsonParams(JSON.toJSONString(cdnDTO));
    	ResponseVo<?> responseVo=deployService.buildByJobName(jobNameBuildParam);
    	if(!responseVo.isSuccess()){
    		return new ResponseResult<>(responseVo.getErrorCode(),responseVo.getMsg());
    	}
    	
		UserDetailDTO userDetailDTO= SsoHelper.currentUser();

    	CdnRecord cdnRecord=new CdnRecord();
    	cdnRecord.setCreateId(userDetailDTO.getId());
    	cdnRecord.setCreateName(userDetailDTO.getRealName());
    	cdnRecord.setCreateTime(new Date());
    	cdnRecord.setDir(cdnDTO.getDir());
    	cdnRecord.setUrl(cdnDTO.getUrl());
		cdnRecordService.insertSelective(cdnRecord);
    	
    	return new ResponseResult<>();
    }
    
    @RequestMapping(value = "/cdnList", method = RequestMethod.GET)
    public ResponseResult cdnList(@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                   @RequestParam(value = "length", defaultValue = "10") int length,
                                   @RequestParam("sortBy") String sortBy,
                                   @RequestParam(value = "descending") String descending) {
    	Page<CdnRecord> page=new Page<CdnRecord>(pageNo, length);
		CdnRecord cdnRecordParam=new CdnRecord();
		Page<CdnRecord> pageList=cdnRecordService.selectPage(page, cdnRecordParam, " create_time desc");
		ResponseVo<List<CdnRecord>> buildRecordVo=new ResponseVo<List<CdnRecord>>(pageList.getTotal(),pageList.getRecords());
        return new ResponseResult<>(buildRecordVo);
    }

}
